package cc.admin.modules.graduate.service.impl;

import cc.admin.modules.graduate.entity.*;
import cc.admin.modules.graduate.mapper.*;
import cc.admin.modules.graduate.service.IExamPaperAnswerSmdService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 试卷作答
 * @Author: cc-admin
 * @Date:   2025-02-25
 * @Version: V1.0.0
 */
@Service
public class ExamPaperAnswerSmdServiceImpl extends ServiceImpl<ExamPaperAnswerSmdMapper, ExamPaperAnswerSmd> implements IExamPaperAnswerSmdService {

    @Autowired
    ChoiceQuestionsMapper choiceQuestionsMapper;

    @Autowired
    MultiChoiceQuestionsMapper multiChoiceQuestionsMapper;

    @Autowired
    DesignQuestionsListMapper designQuestionsListMapper;

    @Autowired
    DesignQuestionsMapper designQuestionsMapper;

    @Autowired
    ExamPaperSmdMapper examPaperSmdMapper;

    @Autowired
    ExamPaperAnswerSmdMapper examPaperAnswerSmdMapper;

    @Autowired
    ChoiceErrorQuestionsMapper choiceErrorQuestionsMapper;

    @Autowired
    DesignAnswerMapper designAnswerMapper;

    @Autowired
    DesignAnswerListMapper designAnswerListMapper;

    @Override
    public void getPaper(List<ExamPaperAnswerSmd> records) {
        Gson gson = new Gson();
        java.lang.reflect.Type stringType = new TypeToken<List<String>>() {}.getType();
        java.lang.reflect.Type optionType = new TypeToken<List<option>>() {}.getType();
        java.lang.reflect.Type multiType = new TypeToken<List<List<String>>>() {}.getType();
        records.forEach(examPaperAnswer -> {
            String choiceIds = examPaperAnswer.getChoiceIds();
            String choiceAnswerJson = examPaperAnswer.getChoiceAnswerJson();
            examPaperAnswer.setChoiceAnswer(gson.fromJson(choiceAnswerJson,stringType));
            String multiChoiceAnswerJson = examPaperAnswer.getMultiChoiceAnswerJson();
            examPaperAnswer.setMultiChoiceAnswer(gson.fromJson(multiChoiceAnswerJson,multiType));

            String designQuestion = examPaperAnswer.getDesignAnswerIds();
            String[] choiceQuestionIds = choiceIds.split(",");

            String[] designQuestionIds = designQuestion.split(",");

            if (choiceQuestionIds.length != 0) {
                List<String> choiceQuestionIdsList = Arrays.stream(choiceQuestionIds).collect(Collectors.toList());
                List<ChoiceQuestions> choiceQuestionsList = choiceQuestionsMapper.selectBatchIds(choiceQuestionIdsList);
                examPaperAnswer.setChoiceQuestionsList(choiceQuestionsList);
            }
            String multiChoiceIds = examPaperAnswer.getMultiChoiceIds();
            String[] multiChoiceQuestionIds = multiChoiceIds.split(",");
            if (multiChoiceQuestionIds.length != 0) {
                List<String> multiQuestionIdsList = Arrays.stream(multiChoiceQuestionIds).collect(Collectors.toList());
                List<MultiChoiceQuestions> multiChoiceQuestionsList = multiChoiceQuestionsMapper.selectBatchIds(multiQuestionIdsList);
                multiChoiceQuestionsList.forEach(multiChoiceQuestions -> {
                    multiChoiceQuestions.setOption(gson.fromJson(multiChoiceQuestions.getOptionJson(), optionType));
                    multiChoiceQuestions.setAnswer(gson.fromJson(multiChoiceQuestions.getAnswerJson(), stringType));
                });
                examPaperAnswer.setMultiChoiceQuestionsList(multiChoiceQuestionsList);
            }
            examPaperAnswer.setMultiChoiceResult(gson.fromJson(examPaperAnswer.getMultiChoiceResultString(),stringType));
            if (designQuestionIds.length != 0) {
                List<String> designQuestionIdsList = Arrays.stream(designQuestionIds).collect(Collectors.toList());
                LambdaQueryWrapper<DesignAnswerList> answerListLambdaQueryWrapper = new LambdaQueryWrapper<>();
                answerListLambdaQueryWrapper.in(DesignAnswerList::getId, designQuestionIdsList);
                answerListLambdaQueryWrapper.orderByAsc(DesignAnswerList::getIndexNum);
                List<DesignAnswerList> DesignQuestionsList = designAnswerListMapper.selectList(answerListLambdaQueryWrapper);
//                List<DesignAnswerList> DesignQuestionsList = designAnswerListMapper.selectBatchIds(designQuestionIdsList);

                DesignQuestionsList.forEach(designAnserListDto -> {
                    //放应用题的子数据
//                    String childIds = designAnserListDto.getChildIds();
//                    List<String> childIdsList = Arrays.stream(childIds.split(",")).collect(Collectors.toList());
//                    List<DesignAnswer> designChildAnswerLists = designAnswerMapper.selectBatchIds(childIdsList);
                    LambdaQueryWrapper<DesignAnswer> queryWrapper = new LambdaQueryWrapper<>();
//                    childIdsList.forEach(childId -> {
//                        queryWrapper.eq(childId!=null&&childId!="",DesignAnswer::getQuestionId,childId);
//                    });
                    queryWrapper.eq(DesignAnswer::getParentId, designAnserListDto.getId());
                    queryWrapper.orderByAsc(DesignAnswer::getIndexNum);


                    List<DesignAnswer> designAnswers = designAnswerMapper.selectList(queryWrapper);
                    //给应用题 答题里放小题
                    designAnserListDto.setQuestionList(designAnswers);
                });
                //试卷里放应用题
                examPaperAnswer.setDesignAnswer(DesignQuestionsList);
            }


        });
    }

    @Override
    @Transactional
    public void updateExamShap(ExamPaperAnswerSmd examPaperAnswer) {

        examPaperAnswer.setType("已审批");
        LambdaQueryWrapper<ExamPaperAnswerSmd> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaperAnswerSmd::getId, examPaperAnswer.getId());
        examPaperAnswerSmdMapper.update(examPaperAnswer, queryWrapper);
        examPaperAnswer.getDesignAnswer().forEach(designAnswerList -> {
            designAnswerList.getQuestionList().forEach(designAnswer -> {

                String shape = designAnswer.getShape();
                if (shape != null) {
                    LambdaQueryWrapper<DesignAnswer> examPaperAnswerLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    examPaperAnswerLambdaQueryWrapper.eq(DesignAnswer::getId, designAnswer.getId());
                    designAnswerMapper.update(designAnswer, examPaperAnswerLambdaQueryWrapper);
                }
            });
        });


    }
}
